<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>有状态组件</title>
		<script src="lib/react/react.development.js"></script>
		<script src="lib/react/react-dom.development.js"></script>
		<script src="lib/babel.min.js"></script>
	</head>
	<body>
		<div id="timer-example"></div>
		<script type="text/babel">
			class Timer extends React.Component {
				constructor(props) {
					super(props);
					this.state = { seconds: 0 };
				}

				tick() {
					this.setState((state) => ({
						seconds: state.seconds + 1,
					}));
				}

				componentDidMount() {
					this.interval = setInterval(() => this.tick(), 1000);
				}

				componentWillUnmount() {
					clearInterval(this.interval);
				}

				render() {
					return <div>Seconds: {this.state.seconds}</div>;
				}
			}

			ReactDOM.render(<Timer />, document.getElementById("timer-example"));
		</script>
	</body>
</html>
